// create a namespace if not already defined
flyui.namespace("flyui.pubmed");

flyui.pubmed.Service = function( endpointURL ) {
	
	/**
	 * @private
	 */
	this._endpoint = endpointURL;
	
};

flyui.pubmed.Publication = function() {
	var that = this ;
	
	this.ncbigene = null;
	
	this.pubID = null;
	
	this.date = "";
	
	this.title = "";
	
	this.mesh = "";
	
	this.genes = "";
	
	this.addGene = function( gene ) {
		flyui.util.appendIfNotMember(that.genes, gene);
		
	};
	
	this.addMesh = function( mesh, termname ) {
//		mesh.termname = termname ;
		flyui.util.appendIfNotMember(that.mesh, mesh);
	};
	
};

flyui.pubmed.Publication.newInstancesFromSPARQLResults = function( resultSet ) {
	var _context = "flyui.pubmed.Publication.newInstancesFromSPARQLResults";
    try {
		var pool = new flyui.pubmed.PublicationPool();
		            
        var bindings = resultSet.results.bindings;
        
        for (var i in bindings) {
        	var binding = bindings[i];
        	
            var pubID = binding.pubID.value;
            
            flyui.debug("==== the pubID " + pubID);                        
            
	    	publication = pool.get(pubID);
//	    	publication.ncbigene = ncbigene;
	    
	    	if (binding.date) {
	        	publication.date = binding.date.value;
	    	}
	    	
	    	if (binding.title) {
	    		publication.title = binding.title.value;
	    	}
	    
	    	if (binding.mesh) {
	    		// each mesh uri is associated with a name
	    		publication.addMesh(binding.mesh.value, binding.termname.value);
	    	}
	        if (binding.gene) {
	            publication.addGene(binding.genes.value);
	        }            
        }
    
        return pool.toArray();
    } catch (error) {
        flyui.debug("wrap and rethrow error to get a stack trace", _context);
        throw new flyui.UnexpectedException(_context, error);
    }    
};

flyui.pubmed.PublicationPool = function() {
	
	/**
	 * @private
	 */
	this._pool = new Object();

};

/**
 * Get a publication from the pool, or create a new one if it doesn't exist.
 * @param {String} pubID 
 * @return a flyui.pubmed.Publication object
 */
flyui.pubmed.PublicationPool.prototype.get = function( pubID ) {
    var _context = "flyui.pubmed.PublicationPool.prototype.get";
    try {
        var publication = this._pool[pubID];
        
        if ( typeof publication == "undefined" || !publication ) {
            publication = new flyui.pubmed.Publication();
            publication.pubID = pubID;
            this._pool[pubID] = publication;   
        }
        
        return publication;
    
    } catch (error) {
        flyui.debug("wrap and rethrow error to get a stack trace", _context);
        throw new flyui.UnexpectedException(_context, error);
    }    	
};

flyui.pubmed.PublicationPool.prototype.toArray = function() {
    var _context = "flyui.pubmed.PublicationPool.prototype.toArray";
    try {
        var array = new Array();
        for (var key in this._pool) {
            array[array.length] = this._pool[key];
        }
        function pubsort(x,y) {
            if (x.date < y.date) return 1;
            else return -1;
        }
        array.sort(pubsort);
        return array;
    } catch (error) {
        flyui.debug("wrap and rethrow error to get a stack trace", _context);
        throw new flyui.UnexpectedException(_context, error);
    }    
};

